require('page/common/header/index.js');
var _util = require('util/util.js')
var _catalog_service = require('service/catalog-service.js');
var _cart_service = require('service/cart-service.js');

var listTemplate = require('./index.string');
const keywords = require('ajv-keywords');
var catalogList = {
    listData: {
        title: '',
        headList: [],
        productList: {},
        itemList: {},
        item: [],
    },
    productHeadList: [
        'Product Id',
        'Name'
    ],
    itemHeadList: [
        'Item ID',
        'Product ID',
        'Description',
        'List Price',
        ''

    ],

    init: function () {

        this.loadAccountInfo();
        this.bindEvents();
        return this;
    },
    bindEvents: function () {

        // $(document).on('click', $('.addToCart'), function (e) {
        //     console.log(e.pageX);
        // });
        $(document).delegate('.addToCart', 'click', function () {
            var itemId = this.id;

            _cart_service.insertItem(itemId,
                function (resp) {
                    if (resp) {
                        if (confirm("添加成功,是否需要跳转到购物车?")) {
                            window.location.href = './cart.html';
                        }
                        console.log('插入成功');
                    }
                    else {
                        console.log('插入失败');
                    }
                },
                function (error) {
                    console.log('error:' + error);
                }
            )
        })
    },

    loadAccountInfo: function () {
        var id = _util.getURLparam('id');
        var route = _util.getURLparam('route');
        var _this = this;


        if (route === 'product') {
            _catalog_service.getProductList(
                id,
                function (res) {
                    console.log(res);
                    _this.listData.headList = _this.productHeadList;
                    _this.listData.title = id;
                    _this.listData.productList = res;
                    _this.listData.itemList = null;
                    _this.listData.item = null;
                    var result = _util.renderHtml(listTemplate, { listData: _this.listData });
                    console.log(result);
                    $('#Catalog').html(result);

                },
                function () {

                }
            );

        }
        else if (route === 'item') {
            console.log(id);
            var productName;
            _catalog_service.getProduct(
                id,
                function (res) {
                    // console.log(res);
                    productName = res.name;
                }
                ,
                function (errMsg) {
                    console.log(errMsg);
                }
            )
            _catalog_service.getItemList(id,
                function (res) {
                    // console.log(res);
                    _this.listData.headList = _this.itemHeadList;
                    _this.listData.title = productName;
                    _this.listData.productList = null;
                    _this.listData.itemList = res;
                    _this.listData.item = null;
                    var result = _util.renderHtml(listTemplate, { listData: _this.listData });
                    // console.log(result);
                    $('#Catalog').html(result);


                },
                function () {


                }
            );

        }
        else if (route === 'detail') {
            console.log(id);
            _catalog_service.getItem(
                id,
                function (resp) {
                    console.log(resp);
                    _this.listData.item = [resp];
                    console.log(_this.listData.item);
                    _this.listData.headList = null;
                    _this.listData.title = null;
                    _this.listData.productList = null;
                    _this.listData.itemList = null;
                    var result = _util.renderHtml(listTemplate, { listData: _this.listData });
                    console.log(result);
                    $('#Catalog').html(result);


                },
                function (error) {

                }
            )
        }
        else if (route === 'search') {

            // console.log(keyword);
            _catalog_service.searchProduct(
                { keyword: id },
                function (res) {
                    _this.listData.headList = null;
                    _this.listData.title = null;
                    _this.listData.productList = res;
                    _this.listData.itemList = null;
                    _this.listData.item = null;
                    var result = _util.renderHtml(listTemplate, { listData: _this.listData });
                    console.log(result);
                    $('#Catalog').html(result);
                },
                function (errMsg) {
                    console.log("errMsg" + errMsg);
                }
            );
        }
    }
};

module.exports = catalogList.init();